package org.zwt.server;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.zwt.common.entity.HttpRequest;
import org.zwt.request.IRequestHandler;
import org.zwt.response.IResponseHandler;
import org.zwt.view.IViewResolver;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Handler;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class HttpServer {
    private Integer port;
    private ServerSocket server;
    private ServerConfig config=new ServerConfig();

    public HttpServer(Integer port) {
        this.port = port;
        try {
            this.server = new ServerSocket(this.port);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void start() throws IOException {
        System.out.println("正在监听" + this.port + "端口.....");
        while (true) {
            Socket socket = this.server.accept();
            new Thread(new ServiceThread(socket,config)).start();
        }

    }


    //修改配置的方法
    public HttpServer setRequestHandler(IRequestHandler requestHandler) {
        this.config.setRequestHandler(requestHandler);
        return this;
    }
    public HttpServer setViewResolver(IViewResolver viewResolver) {
        this.config.setViewResolver(viewResolver);
        return this;

    }
    public HttpServer setResponseHandler(IResponseHandler responseHandler) {
        this.config.setResponseHandler(responseHandler);
        return this;

    }



}
